import { Component,OnInit } from '@angular/core';
import {NavController, NavParams,Events} from 'ionic-angular';
//import { ShowMisQuestionPage } from '../show-misQuestion/show-misQuestion';
import { EditMistakesPage } from '../edit-mistakes/edit-mistakes';
import { Mistake } from '../mistake';
import { PointList } from '../pointList';
import { ShowMistakeByPointPage } from '../show-mistakeByPoint/show-mistakeByPoint';
import { MistakeService } from '../mistake.service';
import { showMistakeByTimePage } from '../show-mistakeByTime/show-mistakeByTime';
import { TempEditMistakesPage } from '../temp-edit-mistake/temp-edit-mistake';
import {LoginNewService} from "../../log-in/login-new.service";

@Component({
    selector: 'page-question-set',
    templateUrl: 'question-set.html',
    providers:[MistakeService],
  })
  export class QuestionSetPage implements OnInit{
    subject:string;//错题集的科目
    questionSet: string = "mistake";
    sortFactor: string = "time";
    currentPoint:PointList = new PointList("point61",1);
    current:string;
    mistakeList: Mistake[];
    mistakeListByTime : Mistake[];
    mistakeListByPoints: Mistake[];
    pointLists:PointList[] = [];
    username : string;


    constructor(public navCtrl:NavController,
                public mistakeService : MistakeService,
                public navaParma:NavParams,
                public events:Events,
                public loginService:LoginNewService
                ){
      this.mistakeList = [];
      this.mistakeListByTime = [];
      this.pointLists = [];
      this.current = " ";
      this.subject = this.navaParma.get("subject");
      this.username = this.navaParma.get("username");
      console.log("跳转后subject："+this.subject);
      this.getMistakesByTime();
    }

    ngOnInit():void{
      //this.getMistake(this.subject);
      //this.getPointList();
     // this.getMistakesByPoints();
    }

    toCamera(subject:string){
      console.log("添加错题前跳转："+this.subject);
      /*let demo = function (mistake:Mistake) {
        return new Promise((resolve, reject) => {
          console.log(mistake);
        })
      }*/
      this.events.subscribe('back',(params)=>{
        console.log("接收数据"+params);
        console.log("mistakes length before："+this.mistakeListByTime.length);
        this.mistakeListByTime.splice(0,0,params);
        console.log("mistakes length after："+this.mistakeListByTime.length);
        this.events.unsubscribe('back');
      })
      this.navCtrl.push(TempEditMistakesPage,{
        subject:subject,
        username:this.username
      });
    }
    gotoDetail(i:number){
      console.log("index is "+ i);
      this.currentPoint = this.pointLists[i];
      console.log(this.currentPoint);
      this.navCtrl.push(EditMistakesPage,{
        currentPoint:this.currentPoint.pointName,
      });
    }

    gotoPointMistakeList(i:number){
      console.log("index is "+ i);
      this.currentPoint = this.pointLists[i];
      console.log(this.currentPoint);
      this.navCtrl.push(ShowMistakeByPointPage,{
        currentPoint:this.currentPoint.pointName,
      });
    }

    gotoShowMistakePage(mistake: Mistake){
      console.log(mistake.misId);
      //this.app.getRootNav().push(nextPage);
      this.navCtrl.push(showMistakeByTimePage,{currentMistakeId : mistake.misId});
    }
    getMistake(subject:string){
      this.mistakeService.getMistakeByCourse(subject,this.loginService.curUser.username).then(data=>this.mistakeList = data,
      error=>console.log(error));
    }

    getMistakesByTime(){
      this.mistakeService.getMistakeByCourse(this.subject,this.username).then(data=>{
        this.mistakeListByTime = data,
        console.log(this.mistakeListByTime)
      });
    }

    getPointList(){
      this.mistakeService.getPointList().then(pointLists=>this.pointLists = pointLists,
      error=>console.log(error));
    }

    showItem(item){
      console.log(item);
    }

   /* getMistakesByPoints(){
      this.mistakeService.getMistakesByPoints("point61").then(data=>this.mistakeListByPoints = data,
      error=>console.log(error));
    }
    */
  }
